Skip to content

Reusing R.Remap type#2860

Closed
RobinTail wants to merge 2 commits into
masterfrom
use-R.Remap-type
Closed

Reusing R.Remap type#2860
RobinTail wants to merge 2 commits into
masterfrom
use-R.Remap-type

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Jul 29, 2025

I suggested this type to Ramda maintainers for R.renameKeys() method

Summary by CodeRabbit

  • Refactor
    • Simplified internal type handling for object remapping, reducing dependencies and streamlining type definitions.
  • Chores
    • Removed unused utility types to improve maintainability.

@RobinTail RobinTail added the refactoring The better way to achieve the same result label Jul 29, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 29, 2025

Walkthrough

This change removes the mapping-helpers.ts file, which provided TypeScript utility types for remapping object keys, and eliminates its usage. The remap method in zod-plugin.ts is updated to use a local Intact type and Ramda's Remap type, simplifying type dependencies and declarations.

Changes

Cohort / File(s) Change Summary
Mapping Helpers Removal
express-zod-api/src/mapping-helpers.ts
Deleted file containing utility types for type-safe remapping and key preservation.
Zod Plugin Refactor
express-zod-api/src/zod-plugin.ts
Removed imports from mapping-helpers, declared a local Intact type, and updated the remap method signature to use Ramda's Remap and the new local type.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ZodObject
    participant Ramda

    User->>ZodObject: Call remap(mapping)
    ZodObject->>Ramda: Use R.Remap to remap keys
    ZodObject->>ZodObject: Apply Intact to preserve keys
    ZodObject-->>User: Return new ZodObject with remapped and intact keys
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~6 minutes

Possibly related PRs

Suggested labels

dependencies

Poem

A hop and a skip, some helpers removed,
Types remapped and signatures smoothed.
No more helpers hiding deep—
The code is now much less steep!
With Ramda’s touch, and types anew,
The rabbit cheers, “Less is more, it’s true!”
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0284b49 and 96662ef.

📒 Files selected for processing (2)
  • express-zod-api/src/mapping-helpers.ts (0 hunks)
  • express-zod-api/src/zod-plugin.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • express-zod-api/src/mapping-helpers.ts
🧰 Additional context used
🧠 Learnings (13)
📓 Common learnings
Learnt from: RobinTail
PR: RobinTail/express-zod-api#0
File: :0-0
Timestamp: 2025-08-01T09:48:13.715Z
Learning: In express-zod-api, when migrating from Zod v3 to v4, the correct approach for internal type imports is to change from `import type { $ZodType } from "zod/v4/core"` to `import { z } from "zod"` and then use `z.core.$ZodType`. The zod/v4/core module is reexported as z.core by the main zod package, making this a valid and working approach.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on @types/qs. This import provides the reference TypeScript needs to infer portable type names.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2736
File: express-zod-api/tsup.config.ts:12-26
Timestamp: 2025-06-14T16:42:52.972Z
Learning: In express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildOptions` callback is intentional behavior and should not be flagged as a side effect issue.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/documentation-helpers.ts:508-512
Timestamp: 2025-05-28T07:58:09.853Z
Learning: In express-zod-api, when working with Zod's JSON schema override callbacks, using `delete` to mutate `ctx.jsonSchema` is the recommended approach per Zod's official documentation, even if it triggers performance linting warnings. This is preferable to creating copies with `undefined` values, especially for snapshot testing.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:75-87
Timestamp: 2025-05-27T20:40:19.548Z
Learning: In express-zod-api's `flattenIO` function in json-schema-helpers.ts, the `additionalProperties` field is used as a template to generate property schemas for literal property names extracted from `propertyNames.const` and `propertyNames.enum`. Converting boolean `additionalProperties` values to empty objects `{}` via `Object(entry.additionalProperties)` is intentional behavior, as the function only needs property schema templates, not the boolean semantics of `additionalProperties`.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: Ramda is correctly listed as a dependency in express-zod-api/package.json, so imports of ramda utilities are properly supported.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: Ramda is correctly listed as a dependency in express-zod-api/package.json, so imports of ramda utilities are properly supported.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/io-schema.ts:5-8
Timestamp: 2025-05-28T05:04:40.327Z
Learning: The Base type `object & { [Symbol.iterator]?: never }` in express-zod-api correctly excludes arrays and other iterables from IOSchema. Arrays are properly rejected because they have a defined Symbol.iterator method which conflicts with the never constraint, as demonstrated by TypeScript compiler errors when attempting to assign z.array() to IOSchema.
📚 Learning: in express-zod-api, when migrating from zod v3 to v4, the correct approach for internal type imports...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#0
File: :0-0
Timestamp: 2025-08-01T09:48:13.715Z
Learning: In express-zod-api, when migrating from Zod v3 to v4, the correct approach for internal type imports is to change from `import type { $ZodType } from "zod/v4/core"` to `import { z } from "zod"` and then use `z.core.$ZodType`. The zod/v4/core module is reexported as z.core by the main zod package, making this a valid and working approach.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: the type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avo...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2428
File: express-zod-api/src/index.ts:44-44
Timestamp: 2025-05-28T18:58:10.064Z
Learning: The type-only import `import type {} from "qs";` in express-zod-api/src/index.ts is necessary to avoid TS2742 errors for exported functions like attachRouting, makeRequestMock, testEndpoint, and testMiddleware that have types depending on @types/qs. This import provides the reference TypeScript needs to infer portable type names.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: in express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildop...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2736
File: express-zod-api/tsup.config.ts:12-26
Timestamp: 2025-06-14T16:42:52.972Z
Learning: In express-zod-api tsup configurations, the direct mutation of `options.supported` in the `esbuildOptions` callback is intentional behavior and should not be flagged as a side effect issue.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: ramda is correctly listed as a dependency in express-zod-api/package.json, so imports of ramda utili...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: Ramda is correctly listed as a dependency in express-zod-api/package.json, so imports of ramda utilities are properly supported.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: zod version 3.25.0 and later expose the zod v4 api through the special import paths "zod/v4" and "zo...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/tests/form-schema.spec.ts:31-31
Timestamp: 2025-05-27T19:27:13.492Z
Learning: Zod version 3.25.0 and later expose the Zod v4 API through the special import paths "zod/v4" and "zod/v4/core", allowing v4 features like .loose() to be used even when the package.json dependency shows a 3.x version.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: in express-zod-api, when working with zod's json schema override callbacks, using `delete` to mutate...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/documentation-helpers.ts:508-512
Timestamp: 2025-05-28T07:58:09.853Z
Learning: In express-zod-api, when working with Zod's JSON schema override callbacks, using `delete` to mutate `ctx.jsonSchema` is the recommended approach per Zod's official documentation, even if it triggers performance linting warnings. This is preferable to creating copies with `undefined` values, especially for snapshot testing.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: in express-zod-api's `flattenio` function in json-schema-helpers.ts, the `additionalproperties` fiel...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:75-87
Timestamp: 2025-05-27T20:40:19.548Z
Learning: In express-zod-api's `flattenIO` function in json-schema-helpers.ts, the `additionalProperties` field is used as a template to generate property schemas for literal property names extracted from `propertyNames.const` and `propertyNames.enum`. Converting boolean `additionalProperties` values to empty objects `{}` via `Object(entry.additionalProperties)` is intentional behavior, as the function only needs property schema templates, not the boolean semantics of `additionalProperties`.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: the jsonschema type is not exported from the main "zod" module and must be imported from "zod/v4/cor...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.015Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: the `./example` directory in the express-zod-api repository contains demonstration code for educatio...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: example/factories.ts:35-42
Timestamp: 2025-05-27T20:03:34.213Z
Learning: The `./example` directory in the express-zod-api repository contains demonstration code for educational purposes only, not intended for production use. Example code can make simplified assumptions for brevity and clarity, and should not be flagged for missing production-level error handling, security measures, or edge case handling.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: the `_zod` property in zod v4 schemas is officially documented and recommended for library authors t...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/metadata.ts:5-13
Timestamp: 2025-05-27T20:08:50.699Z
Learning: The `_zod` property in Zod v4 schemas is officially documented and recommended for library authors to differentiate between Zod 3 and Zod 4 schemas at runtime, despite the underscore prefix. This is explicitly mentioned in Zod's library authors documentation and is not a private internal property.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: the base type `object & { [symbol.iterator]?: never }` in express-zod-api correctly excludes arrays ...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/io-schema.ts:5-8
Timestamp: 2025-05-28T05:04:40.327Z
Learning: The Base type `object & { [Symbol.iterator]?: never }` in express-zod-api correctly excludes arrays and other iterables from IOSchema. Arrays are properly rejected because they have a defined Symbol.iterator method which conflicts with the never constraint, as demonstrated by TypeScript compiler errors when attempting to assign z.array() to IOSchema.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
📚 Learning: in typescript libraries with generic constraints, when providing default values for generic paramete...
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/endpoints-factory.ts:0-0
Timestamp: 2025-05-28T08:35:18.366Z
Learning: In TypeScript libraries with generic constraints, when providing default values for generic parameters, use `as unknown as T` instead of double type assertions like `as SomeType as T`. This makes the unsafe cast explicit and honest about TypeScript's inability to prove constraint compatibility at the generic boundary.

Applied to files:

  • express-zod-api/src/zod-plugin.ts
🔇 Additional comments (2)
express-zod-api/src/zod-plugin.ts (2)

41-41: LGTM! Correct type definition for property exclusion.

The Intact type correctly creates a mapped type that preserves properties from T that don't exist in U. This maintains the same functionality as the previously imported version from mapping-helpers.ts.


72-72: All legacy Remap<Shape, U, V> references have been removed and the migration is complete.

  • Verified no remaining from 'mapping-helpers' imports.
  • No instances of the old three-parameter Remap<*,*,*> remain.
  • Only R.Remap<Shape, U> & Intact<Shape, U> is used in express-zod-api/src/zod-plugin.ts.

No further changes are required—this update cleanly replaces the custom type with Ramda’s built-in Remap plus your Intact helper.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch use-R.Remap-type

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

): z.ZodPipe<
z.ZodPipe<this, z.ZodTransform>, // internal type simplified
z.ZodObject<Remap<Shape, U, V> & Intact<Shape, U>, Config>
z.ZodObject<R.Remap<Shape, U> & Intact<Shape, U>, Config>
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if it's actually exposed/resolved into the build

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not. but there is an option in tsup: dts.resolve, however, it's not documented how exactly it works (it does not)

@RobinTail
Copy link
Copy Markdown
Owner Author

ok, this might require to include @types/ramda into the list of production dependencies, which I don't like

@RobinTail
Copy link
Copy Markdown
Owner Author

I think I found the reason:

--dts-resolve will ignore packages specified in the dependencies field in package.json, meaning they're always externalized

https://dev.to/egoist/rollup-dts-file-using-tsup-2579#fn1

@coveralls-official
Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same
when pulling 96662ef on use-R.Remap-type
into 0284b49 on master.

@RobinTail
Copy link
Copy Markdown
Owner Author

Perhaps it would be better to get back to it if I decide to detach the plugin.

@RobinTail RobinTail closed this Aug 2, 2025
@RobinTail RobinTail mentioned this pull request Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant